/***************************************************
> Copyright (C) 2024 ==QWZL== All rights reserved.
> File Name: land.c
> Author: xiaozhang
> Mail:1990614415@qq.com 
> Created Time: 2024年12月03日 星期二 11时53分24秒
***************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct User
{
	int id;
	char name[32];
	char password[32];
	struct User *next;
}USE;

USE *Builthead(USE *head);
USE *BuiltUser(USE *head);
USE *output(USE *head);
USE *logOn(USE *head);
USE *modify(USE *head);
USE *deluser(USE *head);
void freeuser(USE *head);
int main(int argc,const char* argv[])
{
	printf("\t\t\t----------------------------\n");
	printf("\t\t\t欢迎来到CNC智能仓库管理系统\n");
	printf("\t\t\t----------------------------\n");
	printf("\t\t\t请选择需要进行的项目\n");
	printf("\t\t\t1 -创建新用户-\n");
	printf("\t\t\t2 -登陆用户-\n");
	printf("\t\t\t3 -更改密码-\n");
	printf("\t\t\t4 -删除用户-\n");
	int key = 0;
	USE *head = NULL;
	scanf("%d",&key);
	switch(key)
	{
		case 1:
			head = BuiltUser(head);
			break;
		case 2:
			{
			head = logOn(head);
			output(head);
			}
			break;
		case 3:
			head = modify(head);
			break;
		case 4:
			head = deluser(head);
			break;
		default:
			break;
	}
	freeuser(head);
    return 0;
}
USE *Builthead(USE *head)				//创建头指针
{
	head = (USE *)malloc(sizeof(USE));
	memset(head,0,sizeof(USE));
	if(head == NULL)
	{
		perror("错啦错啦，再看看");
			exit(1);
	}
	return head;
}

USE *BuiltUser(USE *head)            //建立一个新用户
{
//  assert(head != NULL);
	USE *end = head;
	USE *temp = NULL;
	int i = 0;
	int n = 0;
	printf("请输入需要建立n个管理员账号：");
	scanf("%d",&n);
	for(i = 0;i<n;i++)
	{
		temp = (USE *)malloc(sizeof(USE));
		memset(temp,0,sizeof(USE));
		printf("请用电话号码建立用户名");
		scanf("%s",temp->name);
		printf("请输入密码");
		scanf("%s",temp->password);
		int digit = 0;
		int letter = 0;
		int len = strlen(temp->password);

		if(len != 6)
		{
			printf("请设置六位密码，请重新设置密码");
			continue;
		}
		for(int j = 0;j<len;j++)
		{
			if(temp->password[j] >= '0' && temp->password[j] <= '9')
			{
				digit = 1;
			}
			else if((temp->password[j] >= 'a' && temp->password[j] <= 'z')||(temp->password[j] >= 'A' && temp->password[j] <= 'Z'))
			{
				letter = 1;
			}
		}
		if(!(digit && letter))
		{
			printf("密码设置太简单，请重新设置\n");
			continue;
		}
		end->next = temp;
		temp->next = NULL;
		end = end->next;
	}
	return head;
}

USE *output(USE *head)			//打印
{	
	USE *end = head->next;
	int count = 1;
	while(end != NULL)
	{
		end->id = count;
		printf("id:%d\n",end->id);
		printf("name:%s\n",end->name);
		printf("password:%s\n",end->password);
		end = end->next;
		count++;
	}
	return head;
}

USE *logOn(USE *head)			//登陆用户
{
	USE *end = head->next;
	char name[32];
	char password[32];
	printf("input name:");
	scanf("%s",name);
	printf("input password:");
	scanf("%s",password);
	int tryCount = 0;           //记录登陆尝试次数
	while(end != NULL)
	{
		if(strcmp(end->name,name) == 0 && strcmp(end->password,password) == 0)
		{
			printf("登陆成功");
			break;
		}
		end = end->next;
		tryCount++;
	}
	if(tryCount>=3)
	{
		printf("输入错误三次，请重置密码");
		exit(1);
	}       
	if(end == NULL)
	{
		printf("用户名或者密码错误");
	}
	return head;
}

USE *modify(USE *head)			//修改用户密码
{
	USE *end = head -> next;
	char word[20];
	char val[32];				//需要修改密码的用户名
	printf("请输入要修改密码的用户名：");
	scanf("%s",val);
	while(end != NULL)
	{
		if((strcmp(end->name,val))==0)
		{
			printf("输入要修改的密码：");
			scanf("%s",word);
			strcpy(end->password,word);
			printf("密码修改成功！");
			return head;
		}
		end = end->next;
	}
	if(end == NULL)
	{
		printf("本系统没有该管理员，请确认重新输入");
	}
	return head;
}

USE *deluser(USE *head)			//删除用户
{
	USE *end = NULL;
	USE *temp = NULL;
	USE *bef = head;
	end = head->next;
	char userNeme[20]={0};
	printf("请输入需要删除的用户名：");
	scanf("%s",userNeme);
	while(end != NULL)
	{
		if(strcmp(userNeme, end->name) == 0)
		{
			temp = end;
			break;
		}
		bef = end;
		end = end->next;
	}
	if(end == NULL)
	{
		printf("本系统没有该用户信息，请确认用户信息的正确性");
		head = NULL;
	}
	else
	{
		printf("确认是否删除，一旦删除，就再也找不回来了,按下'y/n'");
		char key;
		scanf("%s",&key);
		if(key == 'Y' || key == 'y')
		{
			bef->next = temp->next;
			free(temp);
			temp = NULL;
		}
		else if(key == 'N' || key == 'n')
		{
			printf("放弃删除该用户");
		}
	/*	else
		{
		printf("请选择Y/N，请重新输入");
		scanf("%s",&key);
		return deluser(head);
		} */
	}
	return head;
}

void freeuser(USE *head)		//释放内存空间
{
	USE *temp = NULL;
	while(head != NULL)
	{
		temp = head;
		head = head->next;
		free(temp);
	}
}
